import { TurboModule } from '@rnoh/react-native-openharmony/ts';
import type { TurboModuleContext } from '@rnoh/react-native-openharmony/ts';
import Logger from './Logger';


function createDateFromOptions(options:{hour?: number, minute?: number, is24Hour?: boolean, mode?: 'clock' |'spinner' | 'default'}) {
  // 获取当前日期和时间
  const currentDate = new Date();
  const year = currentDate.getFullYear();
  const month = currentDate.getMonth();
  const day = currentDate.getDate();

  // 解析 options 对象中的小时和分钟
  const hour = typeof options.hour === 'number' ? options.hour : currentDate.getHours();
  const minute = typeof options.minute === 'number' ? options.minute : currentDate.getMinutes();

  // 创建新的 Date 对象
  return new Date(year, month, day, hour, minute);
}

export class TimePickerAndroidModule extends TurboModule {
  constructor(protected ctx: TurboModuleContext) {
    super(ctx);
  }
  // 定义 open 方法
  open(options:{hour?: number, minute?: number, is24Hour?: boolean, mode?: 'clock' |'spinner' | 'default'}): Promise<{
    action: 'timeSetAction' | 'dismissedAction',
    hour?: number,
    minute?: number
  }> {
    return new Promise((resolve) => {
      Logger.info("DatePickerDialog:mode()" + options.mode)
      Logger.info("DatePickerDialog:minDate()" + JSON.stringify(options))
      let selectedTime = createDateFromOptions(options)
      TimePickerDialog.show({
        selected: selectedTime,
        useMilitaryTime: options.is24Hour,
        onAccept: (value) => {

          resolve({
            action: 'timeSetAction',
            hour: value.hour,
            minute: value.minute
          });
        },
        onCancel: () => {
          resolve({
            action: 'dismissedAction'
          });
        },
      });

    });
  }
}